package jp.co.brilliantservice.app.sos;

import oauth.signpost.OAuthProvider;
import oauth.signpost.basic.DefaultOAuthProvider;
import oauth.signpost.commonshttp.CommonsHttpOAuthConsumer;
import twitter4j.ResponseList;
import twitter4j.Twitter;
import twitter4j.TwitterFactory;
import twitter4j.http.AccessToken;
import android.app.ProgressDialog;
import android.content.Context;
import android.content.DialogInterface;
import android.content.DialogInterface.OnCancelListener;

public class TwitterAsyncTask extends android.os.AsyncTask<String,Integer,Boolean> {
	public final static String GET_OAUTH_URL = "get_oauth_url";
	public final static String GET_ACCESS_TOKEN = "get_access_token";
	public final static String POST = "post";
	public final static String GET_FRINENDS_TIMELINE = "get_friends_timeline";
	public final static String SEND_DIRECTMESSAGE = "send_direct_message";
	private final static String consumerKey ="56zF7rSHdFhSBfHeWwNDAw";
	private final static String consumerSecret="SsUZaivxoAMJV0PJnKFTlslpTuK9VZOEK3Y1RUA44";
	public final static String CALLBACKURL="sosbutton://oauth";
	private onRetrieveAuthUrlListener mOnRetrieveAuthUrlListener = null;
	private onRetrieveAccessTokenListener mOnRetrieveAccessTokenListener = null;
	private onPostedListener mOnPostedListener = null;
	private onRetrieveResponseListListener mOnRetrieveResponseListListener = null;

	static private Twitter mTwitter = null;
	static private CommonsHttpOAuthConsumer mConsumer = null;
	private AccessToken mAccessToken = null;
	private String mAuthUrl = null;
	private ProgressDialog mDialog;
	private ResponseList<twitter4j.Status> mList;

//	private Context mContext;

	static private OAuthProvider mProvider = new DefaultOAuthProvider(
			"http://twitter.com/oauth/request_token",
			"http://twitter.com/oauth/access_token",
			"http://twitter.com/oauth/authorize");

	public TwitterAsyncTask(Context context, AccessToken accessToken, boolean noDialog) {
//		mContext = context;
		if (!noDialog) {
			mDialog = new ProgressDialog(context);
		}
		if (accessToken != null) {
			TwitterFactory factory = new TwitterFactory();
			mTwitter = factory.getOAuthAuthorizedInstance(consumerKey, consumerSecret, accessToken);
		}
	}

	@Override
	protected void onPreExecute() {
		if (mDialog!=null) {
	        mDialog.setMessage("Now in progress...");
	        mDialog.setCancelable(true);
	        mDialog.setOnCancelListener(new OnCancelListener() {
				@Override
				public void onCancel(DialogInterface dialog) {
					cancel(true);
				}
			});
	        mDialog.show();
		}
		super.onPreExecute();
	}

	@Override
	protected Boolean doInBackground(String... params) {
		boolean ret = false;
		if (params.length<=0) {
			return false;
		}
		try {
			if (params[0] == GET_OAUTH_URL) {
				mConsumer = new CommonsHttpOAuthConsumer(consumerKey,consumerSecret);
				mAuthUrl = mProvider.retrieveRequestToken(mConsumer,CALLBACKURL);
				ret = true;
			} else if (params[0] == GET_ACCESS_TOKEN) {
				mProvider.retrieveAccessToken(mConsumer,params[1]);
				mAccessToken = new AccessToken(mConsumer.getToken(), mConsumer.getTokenSecret());
				TwitterFactory factory = new TwitterFactory();
				mTwitter = factory.getOAuthAuthorizedInstance(consumerKey, consumerSecret, mAccessToken);
				ret = true;
			} else if (params[0] == POST) {
				mTwitter.updateStatus(params[1]);
				if (mOnPostedListener!=null) {
					mOnPostedListener.onPosted(true);
				}
				ret = true;
			} else if (params[0] == GET_FRINENDS_TIMELINE) {
				mList = mTwitter.getFriendsTimeline();
				ret = true;
			} else if (params[0] == SEND_DIRECTMESSAGE) {
				mTwitter.sendDirectMessage(params[1], params[2]);
				ret = true;
			}
		} catch (Exception e) {
			e.printStackTrace();
		}
		return ret;
	}

	// メインスレッド上で実行される。doinBackgroundの返り値が引数になる
	@Override
	protected void onPostExecute(Boolean result) {
		if (mDialog!=null) {
			mDialog.dismiss();
		}
		if (mOnRetrieveAuthUrlListener!=null) {
			mOnRetrieveAuthUrlListener.onRetrieveAuthUrl(mAuthUrl);
		}
		if (mOnRetrieveAccessTokenListener!=null) {
			mOnRetrieveAccessTokenListener.onRetrieveAccessToken(mAccessToken);
		}
		if (mOnRetrieveResponseListListener!=null) {
			mOnRetrieveResponseListListener.onRetrieveResponseList(mList);
		}
	}

	public void setOnRetrieveAuthUrlListener(onRetrieveAuthUrlListener listener) {
		mOnRetrieveAuthUrlListener = listener;
	}
	public void setOnRetrieveAccessTokenListener(onRetrieveAccessTokenListener listener) {
		mOnRetrieveAccessTokenListener = listener;
	}
	public void setOnPostedListener(onPostedListener listener) {
		mOnPostedListener = listener;
	}
	public void setOnRetrieveResponseListListener(onRetrieveResponseListListener listener) {
		mOnRetrieveResponseListListener = listener;
	}
	public interface onRetrieveAuthUrlListener {
		void onRetrieveAuthUrl(String authUrl);
	}
	public interface onRetrieveAccessTokenListener {
		void onRetrieveAccessToken(AccessToken accessToken);
	}
	public interface onPostedListener {
		void onPosted(boolean success);
	}
	public interface onRetrieveResponseListListener {
		void onRetrieveResponseList(ResponseList<twitter4j.Status> list);
	}
}
